﻿using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.ComponentModel;

namespace Minecraft_Xenon
{
    public class JRESilentInstall
    {
        private string InstallFileName;
        private string InstallPath;
        private bool SlientInstall;
        private bool CreateLog;

        /// <summary>
        /// 安装JRE
        /// 实例化后调用 Start() 安装
        /// </summary>
        /// <param name="installFileName">JRE文件路径（完整）</param>
        /// <param name="installPath">安装路径（完整）</param>
        /// <param name="slient">静默安装</param>
        /// <param name="createLog">生成日志</param>
        public JRESilentInstall(string installFileName, string installPath, bool slient, bool createLog)
        {
            InstallFileName = installFileName;
            InstallPath = installPath;
            SlientInstall = slient;
            CreateLog = createLog; 
        }

        /// <summary>
        /// 开始安装JRE
        /// </summary>
        /// <returns>返回安装信息</returns>
        public string Start()
        {
            try
            {
                //判断安装文件是否存在
                if (!File.Exists(InstallFileName))
                {
                    return "安装文件不存在。";
                }
                //判断安装路径是否存在，不存在则创建路径
                if (!Directory.Exists(InstallPath))
                {
                    Directory.CreateDirectory(InstallPath);
                }

                //NOTICE:修复-2012.10.07-17:21: Java安装Bug修复。
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = InstallFileName;
                psi.CreateNoWindow = true;

                //安装部分
                if (SlientInstall)
                {
                    psi.Arguments = InstallFileName + " /S " + " INSTALLDIR=" + InstallPath;
                }
                else if (CreateLog)
                {
                    psi.Arguments = InstallFileName + " INSTALLDIR=" + InstallPath + " /L" + InstallPath + "\\mx.log";
                }
                else if (SlientInstall && CreateLog)
                {
                    psi.Arguments = InstallFileName + " /S" + " INSTALLDIR=" + InstallPath + " /L" + InstallPath + "\\mx.log";
                }
                else
                {
                    psi.Arguments = InstallFileName + " INSTALLDIR=" + InstallPath;
                }

                Process.Start(psi);

                return "安装成功";
            }
            catch (Win32Exception wine)
            {
                return "安装错误： " + wine.Message;
            }
            catch (ObjectDisposedException ooe)
            {
                return "安装错误： " + ooe.Message;
            }
            catch (IOException ioe)
            {
                return "安装错误，IO错误： " + ioe.Message;
            }
            catch (Exception e)
            {
                return "未知错误。 " + e.Message;
            }
        }

        /// <summary>
        /// 删除JRE安装目录
        /// </summary>
        public void DeleteInstallPath()
        {
            if (Directory.Exists(InstallPath))
            {
                Directory.Delete(InstallPath);
            }
        }

        
    }
}
